﻿using Rsd.Dudu;
using Rsd.Dudu.BizModel;
using Rsd.Dudu.Core;
using Rsd.Dudu.Web;
using Rsd.Dudu.Web.Core;
using Rsd.Dudu.Web.MVC;

using Rsd.Faith.Core;
using Rsd.Faith.Fiscal.Core;
using Rsd.Faith.Models;
using Rsd.Faith.Report.Core;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rsd.Faith.Web.Controllers
{
    /// <summary>
    /// 系统迁移维护API
    /// </summary>
    public class AdminController : Rsd.Faith.Core.FaithController
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [ApiService("初始化", false)]
        public override object Index()
        {
            var config = this.GetObject<SysConfigModel>(obj => obj.BianMa == "GLOBAL");
            if (config == null)
            {
                config = new SysConfigModel();
                config.BianMa = "GLOBAL";
                config.MingCheng = "系统全局配置";
                config.SetState( ObjectStateType.New);
                this.SaveObject(config);
            }
            var items = this.GetObjects<SysConfigItemModel>(obj => obj.ConfigId == config.Key);
            if (items== null || items.Length == 0)
            {
                var list = new List<SysConfigItemModel>();

                list.Add(new SysConfigItemModel( ObjectStateType.New) { ConfigId = config.Key, BianMa="LX_JSFS",MiaoShu="分户利息结算方式",Zhi="0"});
                list.Add(new SysConfigItemModel(ObjectStateType.New) { ConfigId = config.Key, BianMa = "LX_JSRQ", MiaoShu = "分户利息结算日期", Zhi = "12-31" });
                list.Add(new SysConfigItemModel(ObjectStateType.New) { ConfigId = config.Key, BianMa = "LX_DCBL", MiaoShu = "分户计息定存比例(%)", Zhi = "80" });
                list.Add(new SysConfigItemModel(ObjectStateType.New) { ConfigId = config.Key, BianMa = "LX_FXJX", MiaoShu = "是否按复息计息", Zhi = "0" });

                this.SaveObjects(list.ToArray());
            }
            return base.Index();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [ApiService("查看账务报告", true)]
        public object GetReportView()
        {
            var mapping = new Dictionary<string, JsonObject>();
            List<JsonObject> rs_list = null;
            var dt = MyType.GetReportService<ReportZiJinZongZhangModel>().Select(obj => obj.SuoShuQi, "SuoShuQi").Select(obj => obj.ZhuangTai, "ZhuangTai").OrderBy(obj => obj.SuoShuQi, Dudu.DBServices.SortType.ASC).Execute();
            if (dt != null)
            {
                rs_list = dt.ToJsonList();
                foreach (var json in rs_list)
                {
                    mapping.Add(json.Get<string>("SuoShuQi"), json);
                }
            }

            //缴存
            var dt_jc = MyType.GetReportService<FiscalPiaoJuXinXiBiaoModel>().Count(obj => obj.Id, "ShuLiang").GroupBy(obj => obj.SuoShuQi, "SuoShuQi").GroupBy(obj => obj.ZhuangTai, "ZhuangTai").OrderBy(obj => obj.SuoShuQi, Dudu.DBServices.SortType.ASC).Execute();
            if (dt_jc != null)
            {
                var _map = new Dictionary<string, List<JsonObject>>();
                var _list = dt_jc.ToJsonList();
                foreach (var json in _list)
                {
                    var suoShuQi = json.Get<string>("SuoShuQi");

                    if (!_map.ContainsKey(suoShuQi))
                    {
                        _map.Add(suoShuQi, new List<JsonObject>());
                    }
                    json.Add("Text", MyType.GetEnumItem<FaPiaoZhuangTai>(json.Get<string>("ZhuangTai").ConvertTo<FaPiaoZhuangTai>()).Text);
                    _map[suoShuQi].Add(json);
                }
                foreach (var key in _map.Keys)
                {
                    if (!mapping.ContainsKey(key))
                    {
                        mapping.Add(key, new JsonObject());
                        rs_list.Add(mapping[key]);
                    }
                    var json = mapping[key];
                    json.Add("JC", _map[key].ToArray());
                }

            }
            //使用
            var dt_sy = MyType.GetReportService<UsingZiJinShiYongShenQingBiaoModel>().Count(obj => obj.Id, "ShuLiang").GroupBy(obj => obj.SuoShuQi, "SuoShuQi").GroupBy(obj => obj.ZhuangTai, "ZhuangTai").OrderBy(obj => obj.SuoShuQi, Dudu.DBServices.SortType.ASC).Execute();
            if (dt_sy != null)
            {
                var _map = new Dictionary<string, List<JsonObject>>();
                var _list = dt_sy.ToJsonList();
                foreach (var json in _list)
                {
                    var suoShuQi = json.Get<string>("SuoShuQi");

                    if (!_map.ContainsKey(suoShuQi))
                    {
                        _map.Add(suoShuQi, new List<JsonObject>());
                    }
                    json.Add("Text", MyType.GetEnumItem<ZiJinShenQinZhuangTai>(json.Get<string>("ZhuangTai").ConvertTo<ZiJinShenQinZhuangTai>()).Text);
                    _map[suoShuQi].Add(json);
                }
                foreach (var key in _map.Keys)
                {
                    if (!mapping.ContainsKey(key))
                    {
                        mapping.Add(key, new JsonObject());
                        rs_list.Add(mapping[key]);
                    }
                    var json = mapping[key];
                    json.Add("SY", _map[key].ToArray());
                }
            }
            //划拨
            var dt_hb = MyType.GetReportService<UsingZiJinShiYongHuaBoBiaoModel>().Count(obj => obj.Id, "ShuLiang").GroupBy(obj => obj.SuoShuQi, "SuoShuQi").GroupBy(obj => obj.ZhuangTai, "ZhuangTai").OrderBy(obj => obj.SuoShuQi, Dudu.DBServices.SortType.ASC).Execute();
            if (dt_hb != null)
            {
                var _map = new Dictionary<string, List<JsonObject>>();
                var _list = dt_hb.ToJsonList();
                foreach (var json in _list)
                {
                    var suoShuQi = json.Get<string>("SuoShuQi");

                    if (!_map.ContainsKey(suoShuQi))
                    {
                        _map.Add(suoShuQi, new List<JsonObject>());
                    }
                    json.Add("Text", MyType.GetEnumItem<ZiJinHuaBoZhuangTai>(json.Get<string>("ZhuangTai").ConvertTo<ZiJinHuaBoZhuangTai>()).Text);
                    _map[suoShuQi].Add(json);
                }
                foreach (var key in _map.Keys)
                {
                    if (!mapping.ContainsKey(key))
                    {
                        mapping.Add(key, new JsonObject());
                        rs_list.Add(mapping[key]);
                    }
                    var json = mapping[key];
                    json.Add("HB", _map[key].ToArray());
                }
            }
            //增值
            var dt_sr = MyType.GetReportService<BankZengZhiShouYiModel>().Count(obj => obj.Id, "ShuLiang").GroupBy(obj => obj.SuoShuQi, "SuoShuQi").OrderBy(obj => obj.SuoShuQi, Dudu.DBServices.SortType.ASC).Execute();
            if (dt_sr != null)
            {
                var _map = new Dictionary<string, List<JsonObject>>();
                var _list = dt_sr.ToJsonList();
                foreach (var json in _list)
                {
                    var suoShuQi = json.Get<string>("SuoShuQi");

                    if (!_map.ContainsKey(suoShuQi))
                    {
                        _map.Add(suoShuQi, new List<JsonObject>());
                    }
                    json.Add("Text", "共");
                    _map[suoShuQi].Add(json);
                }
                foreach (var key in _map.Keys)
                {
                    if (!mapping.ContainsKey(key))
                    {
                        mapping.Add(key, new JsonObject());
                        rs_list.Add(mapping[key]);
                    }
                    var json = mapping[key];
                    json.Add("SR", _map[key].ToArray());
                }
            }
            var dt_zc = MyType.GetReportService<BankZengZhiShiYongModel>().Count(obj => obj.Id, "ShuLiang").GroupBy(obj => obj.SuoShuQi, "SuoShuQi").GroupBy(obj => obj.ZhuangTai, "ZhuangTai").OrderBy(obj => obj.SuoShuQi, Dudu.DBServices.SortType.ASC).Execute();
            if (dt_zc != null)
            {
                var _map = new Dictionary<string, List<JsonObject>>();
                var _list = dt_zc.ToJsonList();
                foreach (var json in _list)
                {
                    var suoShuQi = json.Get<string>("SuoShuQi");

                    if (!_map.ContainsKey(suoShuQi))
                    {
                        _map.Add(suoShuQi, new List<JsonObject>());
                    }
                    json.Add("Text", MyType.GetEnumItem<ZengZhiShiYongZhuangTai>(json.Get<string>("ZhuangTai").ConvertTo<ZengZhiShiYongZhuangTai>()).Text);
                    _map[suoShuQi].Add(json);
                }
                foreach (var key in _map.Keys)
                {
                    if (!mapping.ContainsKey(key))
                    {
                        mapping.Add(key, new JsonObject());
                        rs_list.Add(mapping[key]);
                    }
                    var json = mapping[key];
                    json.Add("ZC", _map[key].ToArray());
                }
            }
            //小区
            var dt_xq = MyType.GetReportService<ReportXiaoQuZhangMuModel>().Count(obj => obj.Id, "ShuLiang").GroupBy(obj => obj.SuoShuQi, "SuoShuQi").GroupBy(obj => obj.ZhuangTai, "ZhuangTai").OrderBy(obj => obj.SuoShuQi, Dudu.DBServices.SortType.ASC).Execute();
            if (dt_xq != null)
            {
                var _map = new Dictionary<string, List<JsonObject>>();
                var _list = dt_xq.ToJsonList();
                foreach (var json in _list)
                {
                    var suoShuQi = json.Get<string>("SuoShuQi");

                    if (!_map.ContainsKey(suoShuQi))
                    {
                        _map.Add(suoShuQi, new List<JsonObject>());
                    }
                    json.Add("Text", MyType.GetEnumItem<ZhangMuZhuangTai>(json.Get<string>("ZhuangTai").ConvertTo<ZhangMuZhuangTai>()).Text);
                    _map[suoShuQi].Add(json);
                }
                foreach (var key in _map.Keys)
                {
                    if (!mapping.ContainsKey(key))
                    {
                        mapping.Add(key, new JsonObject());
                        rs_list.Add(mapping[key]);
                    }
                    var json = mapping[key];
                    json.Add("XQ", _map[key].ToArray());
                }
            }
            //楼栋
            var dt_ld = MyType.GetReportService<ReportLouDongZhangMuModel>().Count(obj => obj.Id, "ShuLiang").GroupBy(obj => obj.SuoShuQi, "SuoShuQi").GroupBy(obj => obj.ZhuangTai, "ZhuangTai").OrderBy(obj => obj.SuoShuQi, Dudu.DBServices.SortType.ASC).Execute();
            if (dt_ld != null)
            {
                var _map = new Dictionary<string, List<JsonObject>>();
                var _list = dt_ld.ToJsonList();
                foreach (var json in _list)
                {
                    var suoShuQi = json.Get<string>("SuoShuQi");

                    if (!_map.ContainsKey(suoShuQi))
                    {
                        _map.Add(suoShuQi, new List<JsonObject>());
                    }
                    json.Add("Text", MyType.GetEnumItem<ZhangMuZhuangTai>(json.Get<string>("ZhuangTai").ConvertTo<ZhangMuZhuangTai>()).Text);
                    _map[suoShuQi].Add(json);
                }
                foreach (var key in _map.Keys)
                {
                    if (!mapping.ContainsKey(key))
                    {
                        mapping.Add(key, new JsonObject());
                        rs_list.Add(mapping[key]);
                    }
                    var json = mapping[key];
                    json.Add("LD", _map[key].ToArray());
                }
            }
            //单元
            var dt_dy = MyType.GetReportService<ReportLouDongZhangMuModel>().Count(obj => obj.Id, "ShuLiang").GroupBy(obj => obj.SuoShuQi, "SuoShuQi").GroupBy(obj => obj.ZhuangTai, "ZhuangTai").OrderBy(obj => obj.SuoShuQi, Dudu.DBServices.SortType.ASC).Execute();
            if (dt_dy != null)
            {
                var _map = new Dictionary<string, List<JsonObject>>();
                var _list = dt_dy.ToJsonList();
                foreach (var json in _list)
                {
                    var suoShuQi = json.Get<string>("SuoShuQi");

                    if (!_map.ContainsKey(suoShuQi))
                    {
                        _map.Add(suoShuQi, new List<JsonObject>());
                    }
                    json.Add("Text", MyType.GetEnumItem<ZhangMuZhuangTai>(json.Get<string>("ZhuangTai").ConvertTo<ZhangMuZhuangTai>()).Text);

                    _map[suoShuQi].Add(json);
                }
                foreach (var key in _map.Keys)
                {
                    if (!mapping.ContainsKey(key))
                    {
                        mapping.Add(key, new JsonObject());
                        rs_list.Add(mapping[key]);
                    }
                    var json = mapping[key];
                    json.Add("DY", _map[key].ToArray());
                }
            }
            //分户
            foreach (var item in rs_list)
            {
                if (string.IsNullOrEmpty(item.Get<string>("SuoShuQi")))
                {
                    continue;
                }

                var dt_lx = MyType.GetReportService<ReportFenHuZhangMuModel>(item.Get<string>("SuoShuQi")).Count(obj => obj.Id, "ShuLiang").GroupBy(obj => obj.SuoShuQi, "SuoShuQi").GroupBy(obj => obj.ZhuangTai, "ZhuangTai").OrderBy(obj => obj.SuoShuQi, Dudu.DBServices.SortType.ASC).Execute();

                var _list = dt_lx.ToJsonList();
                foreach (var json in _list)
                {
                    json.Add("Text", MyType.GetEnumItem<ZhangMuZhuangTai>(json.Get<string>("ZhuangTai").ConvertTo<ZhangMuZhuangTai>()).Text);
                }
                item.Add("FH", _list.ToArray());
            }

            //利息
            foreach (var item in rs_list)
            { 
                if (string.IsNullOrEmpty(item.Get<string>("SuoShuQi")))
                {
                    continue;
                }

                var dt_lx = MyType.GetReportService<BankFenHuShouYiBiaoModel>(item.Get<string>("SuoShuQi")).Count(obj => obj.Id, "ShuLiang").GroupBy(obj => obj.SuoShuQi, "SuoShuQi").GroupBy(obj => obj.ZhuangTai, "ZhuangTai").OrderBy(obj => obj.SuoShuQi, Dudu.DBServices.SortType.ASC).Execute();
               
                var _list = dt_lx.ToJsonList();
                foreach (var json in _list)
                { 
                    json.Add("Text", MyType.GetEnumItem<LiXiShouYiZhuangTai>(json.Get<string>("ZhuangTai").ConvertTo<LiXiShouYiZhuangTai>()).Text); 
                }
                item.Add("LX", _list.ToArray());
            }

            return rs_list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="group"></param>
        /// <param name="rebuild"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        [PramaRule("rebuild", "是否清楚历史数据(rebuild)", typeof(bool), false, false)]
        [PramaRule("group", "分组(group)", typeof(string))]
        [PramaRule("pwd", "密码(pwd)", typeof(string))]
        [ApiService("重新建账", true)]
        public object Rebuild(string group, bool rebuild, string pwd)
        {
            object rs = 0;
            switch (group.ToUpper())
            {
                case "XJSQ":
                    rs = this.RebuildSQ(rebuild, pwd);
                    break;
                case "JCRZ":
                    rs = this.RebuildJC(rebuild, pwd);
                    break;
                case "SYRZ":
                    rs = this.RebuildSY(rebuild, pwd);
                    break;
                case "FTHC":
                    rs = this.RebuildFTHC(rebuild, pwd);
                    break;
                case "ZZHS":
                    rs = this.ReportZongZhangHS(rebuild, pwd);
                    break;
                case "XQHS":
                    rs = this.ReportXiaoQuHS(rebuild, pwd);
                    break;
                case "LDHS":
                    rs = this.ReportLouDongHS(rebuild, pwd);
                    break;
                case "DYHS":
                    rs = this.ReportDanYuanHS(rebuild, pwd);
                    break;
                case "FHHS":
                    rs = this.ReportFenHuHS(rebuild, pwd);
                    break;
                default:
                    rs = -1;
                    break;
            }
            return rs;
        }

        #region API生成
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rebuild"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        private object RebuildSQ(bool rebuild, string pwd)
        {
            if (pwd.ToLower() != "admin@redmicro.cn8723")
            {
                return new ErrorResult(-1, "权限错误", null);
            }
            var group = "XJSQ";

            if (rebuild)
            {
                var apiList = new List<ApiTimerModel>();
                //清楚历史数据
                MyType.GetDBOperator<ApiTimerModel>().Delete().Where(obj => obj.Group.StartsWith(group)).Execute<int>();
                for (var year = 2000; year < DateTime.Now.Year; year++)
                {
                    apiList.Add(new ApiTimerModel(Dudu.BizModel.ObjectStateType.New)
                    {
                        Group = group,
                        Text = "新建所属期（" + year.ToString() + "）",
                        Path = "Admin/NewPeriod?suoShuQi=" + year.ToString()
                    });
                }
                //
                this.SaveObjects(apiList.ToArray());
            }
            else
            {
                MyType.GetDBOperator<ApiTimerModel>().Update(obj=>obj.Url,"").Update<bool>(obj=>obj.Enabled,false).Update(obj=>obj.Success,0).Update(obj=>obj.SortIndex,0).Where(obj => obj.Group.StartsWith(group)).Execute<int>();
            }
               
            return MyType.GetDBOperator<ApiTimerModel>().Count().Where(obj => obj.Group.StartsWith(group)).Execute<int>();
        }
      
        /// <summary>
        /// 生成 按小区将缴存数据入账的Api请求
        /// </summary>
        /// <param name="rebuild"></param>
        /// <param name="pwd"></param>
        /// <returns></returns> 
        private object RebuildJC(bool rebuild,string pwd)
        {

            if (pwd.ToLower() != "admin@redmicro.cn8723")
            {
                return new ErrorResult(-1, "权限错误", null);
            }

            var group = "JCRZ";
            if (rebuild)
            { 
                var dt = MyType.GetReportService<FiscalPiaoJuXinXiBiaoModel>().Select(obj => obj.FaPiaoBianHao, "FaPiaoBianHao", false) 
                .Where(obj => obj.ZhuangTai.ToString() == FaPiaoZhuangTai.RZ.ToString())//仅处理已入账数据
                .Execute();
                 
                var apiList = new List<ApiTimerModel>();
                
                // ===建账
                for (var i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];
                    var bianHao = r["FaPiaoBianHao"].ToString(); 
                    if (string.IsNullOrEmpty(bianHao))
                    {
                        continue;
                    }

                    // 缴存入账：总账，小区帐，楼栋明细帐，单元明细帐，分户帐 
                    apiList.Add(new ApiTimerModel( Dudu.BizModel.ObjectStateType.New) {
                        Group = group + bianHao,
                        Text = "发票(编号:" + bianHao + ")缴存入账",
                        Path = "Fiscal/JiaoCunRuZhangZZ?faPiaoHaoMa=" + bianHao
                    });
                }

                //清楚历史数据
                MyType.GetDBOperator<ApiTimerModel>().Delete().Where(obj => obj.Group.StartsWith( group)).Execute<int>();
                //
                this.SaveObjects(apiList.ToArray());
            }
            else
            {
                MyType.GetDBOperator<ApiTimerModel>().Update(obj => obj.Url, "").Update<bool>(obj => obj.Enabled, false).Update(obj => obj.Success, 0).Update(obj => obj.SortIndex, 0).Where(obj => obj.Group.StartsWith(group)).Execute<int>();
            }
            return MyType.GetDBOperator<ApiTimerModel>().Count().Where(obj => obj.Group.StartsWith(group)).Execute<int>();
        }

        /// <summary>
        /// 分摊数据核查
        /// </summary>
        /// <param name="rebuild"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        private object RebuildFTHC(bool rebuild, string pwd)
        {
            if (pwd.ToLower() != "admin@redmicro.cn8723")
            {
                return new ErrorResult(-1, "权限错误", null);
            }
            var group = "FTHC";
            if (rebuild)
            {
                var apiList = new List<ApiTimerModel>();
                var list = this.GetObjects<UsingZiJinShiYongHuaBoBiaoModel>(null);

                foreach (var huaBo in list)
                { 
                    apiList.Add(new ApiTimerModel(Dudu.BizModel.ObjectStateType.New)
                    {
                        Group = group + "HB" + huaBo.ShenQingDanHao,
                        Text = "申请单" + huaBo.ShenQingDanHao + "分摊数据核查",
                        Path = "Fiscal/FenTanZhangHuJianCha?shenQingDanHao=" + huaBo.ShenQingDanHao
                    }); 
                }

                //清楚历史数据
                MyType.GetDBOperator<ApiTimerModel>().Delete().Where(obj => obj.Group.StartsWith(group)).Execute<int>();
                //
                this.SaveObjects(apiList.ToArray());
            }
            else
            {
                MyType.GetDBOperator<ApiTimerModel>().Update(obj => obj.Url, "").Update<bool>(obj => obj.Enabled, false).Update(obj => obj.Success, 0).Update(obj => obj.SortIndex, 0).Where(obj => obj.Group.StartsWith(group)).Execute<int>();
            }
             
            return MyType.GetDBOperator<ApiTimerModel>().Count().Where(obj => obj.Group.StartsWith(group)).Execute<int>();
        }

        /// <summary>
        /// 生成 按使用申请单号入账的Api请求
        /// </summary>
        /// <param name="rebuild"></param>
        /// <param name="pwd"></param>
        /// <returns></returns> 
        private object RebuildSY(bool rebuild, string pwd)
        {
            if (pwd.ToLower() != "admin@redmicro.cn8723")
            {
                return new ErrorResult(-1, "权限错误", null);
            }
            var group = "SYRZ";
            if (rebuild)
            {
                var apiList = new List<ApiTimerModel>();
                var list = this.GetObjects<UsingZiJinShiYongHuaBoBiaoModel>(null);

                foreach (var huaBo in list)
                {
                    // 使用入账（划拨，分摊，入账）
                    apiList.Add(new ApiTimerModel(Dudu.BizModel.ObjectStateType.New)
                    {
                        Group = group + "FT" + huaBo.ShenQingDanHao,
                        Text = "使用申请(单号:" + huaBo.ShenQingDanHao + ")入账",
                        Path = "Fiscal/ShiYongRuZhang?shenQingDanHao=" + huaBo.ShenQingDanHao
                    });

                    // 派息入账：小区帐 [派息没有确切数据需要手动操作]

                    // 结息入账：分户帐 [结息没有确切数据需要手动操作]

                }

                //清楚历史数据
                MyType.GetDBOperator<ApiTimerModel>().Delete().Where(obj => obj.Group.StartsWith(group)).Execute<int>();
                //
                this.SaveObjects(apiList.ToArray());
            }
            else
            {
                MyType.GetDBOperator<ApiTimerModel>().Update(obj => obj.Url, "").Update<bool>(obj => obj.Enabled, false).Update(obj => obj.Success, 0).Update(obj => obj.SortIndex, 0).Where(obj => obj.Group.StartsWith(group)).Execute<int>();
            }


            return MyType.GetDBOperator<ApiTimerModel>().Count().Where(obj => obj.Group.StartsWith(group)).Execute<int>();
        }

        /// <summary>
        /// 生成 按所属期总账账目核算的Api请求
        /// </summary>
        /// <param name="rebuild"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>]
        private object ReportZongZhangHS(bool rebuild, string pwd)
        {
            if (pwd.ToLower() != "admin@redmicro.cn8723")
            {
                return new ErrorResult(-1, "权限错误", null);
            }
            var group = "ZZHS";
            if (rebuild)
            {
                var apiList = new List<ApiTimerModel>();
                // 总账核算：账目 （期初、余额）
                for (var year = 2000; year < DateTime.Now.Year; year++)
                {
                    // === 核算 按所属期由小到大 顺序核算 
                    apiList.Add(new ApiTimerModel(Dudu.BizModel.ObjectStateType.New) {
                        Group = group + year,
                        Text = "资金总账" + year.ToString() + "期核算",
                        Path = "Report/HeSuanZiJinZongZhang?suoShuQi=" + year.ToString()
                    });
                }
                //清楚历史数据
                MyType.GetDBOperator<ApiTimerModel>().Delete().Where(obj => obj.Group.StartsWith(group)).Execute<int>();
                //
                this.SaveObjects(apiList.ToArray());
            }
            else
            {
                MyType.GetDBOperator<ApiTimerModel>().Update(obj => obj.Url, "").Update<bool>(obj => obj.Enabled, false).Update(obj => obj.Success, 0).Update(obj => obj.SortIndex, 0).Where(obj => obj.Group.StartsWith(group)).Execute<int>();
            }
            return MyType.GetDBOperator<ApiTimerModel>().Count().Where(obj => obj.Group.StartsWith(group)).Execute<int>();
        }

        /// <summary>
        /// 生成 按小区的账目核算的Api请求
        /// </summary>
        /// <param name="rebuild"></param>
        /// <param name="pwd"></param>
        /// <returns></returns> 
        private object ReportXiaoQuHS(bool rebuild, string pwd)
        {
            if (pwd.ToLower() != "admin@redmicro.cn8723")
            {
                return new ErrorResult(-1, "权限错误", null);
            }
            var group = "XQHS";
            if (rebuild)
            {
                var apiList = new List<ApiTimerModel>();

                var list = this.GetObjects<InfoXiaoQuWuYeBiaoModel>(null);
              

                foreach (var xiaoqu in list)
                {
                    // 小区总帐： 账目 （期初 、 余额）
                    apiList.Add(new ApiTimerModel(Dudu.BizModel.ObjectStateType.New) {
                        Group = group + xiaoqu.BianHao,
                        Text = "小区(" + xiaoqu.BianHao + ")总账所有属期核算",
                        Path = "Report/HeSuanXiaoQuZongZhang?xiaoQuBianHao=" + xiaoqu.BianHao
                    });
                    

                }
                //清楚历史数据
                MyType.GetDBOperator<ApiTimerModel>().Delete().Where(obj => obj.Group.StartsWith( group)).Execute<int>();
                //
                this.SaveObjects(apiList.ToArray());
            }
            else
            {
                MyType.GetDBOperator<ApiTimerModel>().Update(obj => obj.Url, "").Update<bool>(obj => obj.Enabled, false).Update(obj => obj.Success, 0).Update(obj => obj.SortIndex, 0).Where(obj => obj.Group.StartsWith(group)).Execute<int>();
            }

            return MyType.GetDBOperator<ApiTimerModel>().Count().Where(obj => obj.Group.StartsWith(group)).Execute<int>();
        }

        /// <summary>
        /// 生成 按小区楼栋的账目核算的Api请求
        /// </summary>
        /// <param name="rebuild"></param>
        /// <param name="pwd"></param>
        /// <returns></returns> 
        private object ReportLouDongHS(bool rebuild, string pwd)
        {
            if (pwd.ToLower() != "admin@redmicro.cn8723")
            {
                return new ErrorResult(-1, "权限错误", null);
            }
            var group = "LDHS";
            if (rebuild)
            {
                var apiList = new List<ApiTimerModel>();

                var list = this.GetObjects<InfoXiaoQuWuYeBiaoModel>(null);

                foreach (var xiaoqu in list)
                {
                    
                    // 楼栋明细帐： 账目 （期初 、 余额） 
                    apiList.Add(new ApiTimerModel(Dudu.BizModel.ObjectStateType.New)
                    {
                        Group = group + xiaoqu.BianHao,
                        Text = "小区(" + xiaoqu.BianHao + ")楼栋明细帐所有属期核算",
                        Path = "Report/HeSuanLouDongZhang?xiaoQuBianHao=" + xiaoqu.BianHao
                    });

                }
                //清楚历史数据
                MyType.GetDBOperator<ApiTimerModel>().Delete().Where(obj => obj.Group.StartsWith(group)).Execute<int>();
                //
                this.SaveObjects(apiList.ToArray());
            }
            else
            {
                MyType.GetDBOperator<ApiTimerModel>().Update(obj => obj.Url, "").Update<bool>(obj => obj.Enabled, false).Update(obj => obj.Success, 0).Update(obj => obj.SortIndex, 0).Where(obj => obj.Group.StartsWith(group)).Execute<int>();
            }

            return MyType.GetDBOperator<ApiTimerModel>().Count().Where(obj => obj.Group.StartsWith(group)).Execute<int>();
        }

        /// <summary>
        /// 生成 按小区单元的账目核算的Api请求
        /// </summary>
        /// <param name="rebuild"></param>
        /// <param name="pwd"></param>
        /// <returns></returns> 
        private object ReportDanYuanHS(bool rebuild, string pwd)
        {
            if (pwd.ToLower() != "admin@redmicro.cn8723")
            {
                return new ErrorResult(-1, "权限错误", null);
            }
            var group = "DYHS";
            if (rebuild)
            {
                var apiList = new List<ApiTimerModel>();

                var list = this.GetObjects<InfoXiaoQuWuYeBiaoModel>(null);


                foreach (var xiaoqu in list)
                {
                    // 单元明细帐： 账目 （期初 、 余额） 
                    apiList.Add(new ApiTimerModel(Dudu.BizModel.ObjectStateType.New)
                    {
                        Group = group + xiaoqu.BianHao,
                        Text = "小区(" + xiaoqu.BianHao + ")单元明细帐所有属期核算",
                        Path = "Report/HeSuanDanYuanZhang?xiaoQuBianHao=" + xiaoqu.BianHao
                    });

                }
                //清楚历史数据
                MyType.GetDBOperator<ApiTimerModel>().Delete().Where(obj => obj.Group.StartsWith(group)).Execute<int>();
                //
                this.SaveObjects(apiList.ToArray());
            }
            else
            {
                MyType.GetDBOperator<ApiTimerModel>().Update(obj => obj.Url, "").Update<bool>(obj => obj.Enabled, false).Update(obj => obj.Success, 0).Update(obj => obj.SortIndex, 0).Where(obj => obj.Group.StartsWith(group)).Execute<int>();
            }

            return MyType.GetDBOperator<ApiTimerModel>().Count().Where(obj => obj.Group.StartsWith(group)).Execute<int>();
        }

        /// <summary>
        /// 分户账目指定所属期核算
        /// </summary>
        /// <param name="pwd"></param>
        /// <param name="type"></param>
        /// <returns></returns> 
        private object ReportFenHuHS(bool rebuild, string pwd)
        {
            if (pwd.ToLower() != "admin@redmicro.cn8723")
            {
                return new ErrorResult(-1, "权限错误", null);
            }
            var group = "FHHS" ;
            if (rebuild)
            {
                var apiList = new List<ApiTimerModel>();
                 
                var list = this.GetObjects<InfoXiaoQuWuYeBiaoModel>(null);

                // 分户账目
                foreach (var xiaoqu in list)
                {
                    apiList.Add(new ApiTimerModel(Dudu.BizModel.ObjectStateType.New)
                    {
                        Group = group + xiaoqu.BianHao,
                        Text = "小区(" + xiaoqu.BianHao + ")分户账目所有属期核算",
                        Path = "Report/HeSuanFenHuZhang?&xiaoQuBianHao=" + xiaoqu.BianHao
                    });
                }
                //清楚历史数据
                MyType.GetDBOperator<ApiTimerModel>().Delete().Where(obj => obj.Group.StartsWith( group)).Execute<int>();
                //
                this.SaveObjects(apiList.ToArray());
            }
            else
            {
                MyType.GetDBOperator<ApiTimerModel>().Update(obj => obj.Url, "").Update<bool>(obj => obj.Enabled, false).Update(obj => obj.Success, 0).Update(obj => obj.SortIndex, 0).Where(obj => obj.Group.StartsWith(group)).Execute<int>();
            }

            return MyType.GetDBOperator<ApiTimerModel>().Count().Where(obj => obj.Group.StartsWith( group)).Execute<int>();
        }

        #endregion
    }
}
